package com.csx.midware.redis;

import redis.clients.jedis.Jedis;

/**
 * redis 做分布式自增器
 */
public class RedisCounterExample {

    private Jedis jedis;

    public RedisCounterExample() {
        jedis = new Jedis("localhost");
    }

    public void incrementCounter(String key) {
        jedis.incr(key);
    }

    public long getCounter(String key) {
        return jedis.get(key) != null ? Long.parseLong(jedis.get(key)) : 0;
    }

    public static void main(String[] args) {
        RedisCounterExample example = new RedisCounterExample();
        example.incrementCounter("visitCount");
        System.out.println("Visit Count: " + example.getCounter("visitCount"));
    }
}